import Vue from "vue"
//1.引入vuex
import Vuex, {
    Store
} from "vuex"

//2.注册vuex
Vue.use(Vuex)
    //3.实例化vuex
export default new Store({
    state: {
        address_id: JSON.parse(localStorage.getItem("address_id") || "[]"),
        address_data_list: JSON.parse(localStorage.getItem('address_data_list') || '[]'),
        search_history: JSON.parse(localStorage.getItem("search_history") || "[]"),
        cartList: JSON.parse(localStorage.getItem('cartList')) || [],
        count: 0,
        city: '长沙',
    },
    mutations: {
        // 添加地址信息
        sava_address_data(state, address_data_info) {
            //加载地址列表
            var addresslist = state.address_data_list;
            addresslist.push(address_data_info)
            this.address_data_list = "[]"
                // 将地址列表存入仓库
            localStorage.setItem('address_data_list', JSON.stringify(addresslist))
                //   console.log(addresslist)
        },
        // 编辑保存的地址信息
        edit_address_data(state, edit_address_info) {
            // console.log(edit_address_info)
            localStorage.setItem('address_data_list', JSON.stringify(edit_address_info))
        },
        address_id_add(state) {
            if (state.address_id) {
                state.address_id++
            } else {
                state.address_id = 1
            }
        },
        changeCity(state, cityname) {
            state.city = cityname
        },
        set_search_history(state, searchinfo) {
            // 搜索时保存数据
            localStorage.setItem('search_history', JSON.stringify(searchinfo))
        },
        clear_history(state) {
            state.search_history = []
            localStorage.setItem('search_history', JSON.stringify(state.search_history))
        },
        // 将之前的默认地址全部设为false
        changedaddress(state) {
            var list = []
            state.address_data_list.forEach((el, index) => {
                el.isDefault = false;
                list.push(el)
            })
            localStorage.setItem('address_data_list', JSON.stringify(list))
        },
        addCart(state, foods) {
            // console.log(foods);
            let flag = false
            state.cartList.forEach(item => {
                if (item.id === foods.id) {
                    item.num++
                        flag = true
                }
            })
            if (!flag) {
                state.cartList.push(foods)
            }
            localStorage.setItem("cartList", JSON.stringify(state.cartList))
        },
        updateCount(state, updateMsg) {
            let {
                val,
                det: {
                    name
                }
            } = updateMsg;
            // console.log(val, name)
            state.cartList.some(item => {
                if (item.id === name) {
                    item.num = val
                }
            })
            localStorage.setItem("cartList", JSON.stringify(state.cartList))
        },
        clearAll(state) {
            state.cartList = [];
            localStorage.setItem("cartList", JSON.stringify(state.cartList))
        },
        deleteCartItem(state, index) {
            // console.log(index)
            state.cartList.splice(index, 1);
            localStorage.setItem("cartList", JSON.stringify(state.cartList))
        }
    },
    actions: {
        sava_address_data({
            commit
        }, address_data_info) {
            commit('sava_address_data', address_data_info)
        },
        changeCity({
            commit
        }, cityname) {
            commit('changeCity', cityname)
        },
        addCart({
            commit
        }, foods) {
            commit('addCart', foods)
        },
        updateCount({
            commit
        }, updateMsg) {
            commit('updateCount', updateMsg)
        },
        clearAll({
            commit
        }) {
            commit('clearAll')
        },
        deleteCartItem({ commit }, index) {
            commit('deleteCartItem', index)
        }
    },
    getters: {
        getAllCount(state) {
            let count = 0;
            state.cartList.forEach(item => {
                count += item.num;
            })
            return count;
        },
        getTotal(state) {
            let total = 0;
            state.cartList.forEach(item => {
                total += item.num * item.price
            });
            return total;
        },
        getCurrentAddress(state) {
            let address = '';
            state.address_data_list.forEach(item => {
                if (item.isDefault === true) {
                    address = item.province + item.city + item.county + item.address;
                }
            });
            return address;
        }
    }
})